﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zoo.Cat.Application.Authentication;
using Zoo.Cat.Application.Jobseekers;
using Zoo.Cat.Domain.Entities.Jobseekers;
using Zoo.Cat.Web.Models.Jobseeker;
using Zoo.Cat.Web.Framework;
//using Zoo.Infrastructure;

//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Web;
//using System.Web.Mvc;
//using Zoo.Cat.Application.Polls;
//using Zoo.Cat.Web.Framework;
//using Zoo.Cat.Application.Authentication;
//using Zoo.Cat.Application.Jobseekers;

namespace Zoo.Cat.Web.Controllers {
    public class JobseekerController : Controller {

        #region Fields
        private readonly IWorkContext _workContext;
        private readonly IAuthenticationService _authenticationService;
        private readonly IJobseekerService _jobseekerService;
        private readonly IJobseekerRegistrationService _jobseekerRegistrationService;
        #endregion

        #region Ctor
        public JobseekerController(IAuthenticationService authenticationService,
             IWorkContext workContext,
            IJobseekerService jobseekerService,
             IJobseekerRegistrationService jobseekerRegistrationService
            ) {
            _authenticationService = authenticationService;
            _workContext = workContext;
            _jobseekerService = jobseekerService;
            _jobseekerRegistrationService = jobseekerRegistrationService;
        }
        #endregion



        #region Login/logout/register

        public ActionResult Login() {
            var model = new LoginModel();
            model.Email = "ddd";
            return View(model);
        }

        public ActionResult Index() {
           return  View();
        }


        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl) {
            if (ModelState.IsValid) {
                if (_jobseekerRegistrationService.ValidateUser(model.Email, model.Password)) {
                    Jobseeker jobseeker =_jobseekerService.GetJobseekerByEmail(model.Email);

                    // sign in
                   _authenticationService.SignIn(jobseeker, model.RememberMe);

                   if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                       return Redirect(returnUrl);
                   else
                       return RedirectToAction("Index", "Home");

                 

                }
                else {
                    ModelState.AddModelError("","Account.Login.WrongCredentials");
                }

                return View(model);
            }
            return View();
        }

        #endregion
    }
}